package com.yolov8.controller;


import com.yolov8.config.AppConfig;
import com.yolov8.service.DetectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/mission")
public class MissionController {

    @Autowired
    private DetectionService detectionService;

    @Autowired
    private AppConfig app;

    @GetMapping("/start")
    public String start(@RequestParam String pushUrl) throws IOException {
        String taskId = "task1";
        List<String> classNames = app.getModel().getClassNames();
        //log.info("配置信息: classNames:{}", classNames);
        System.out.println("classNames:"+classNames);

        // 获取模型资源
        String modelPath = app.getModel().getDirectory();
        String pullUrl = app.getModel().getPullUrl();
        log.info("配置信息: start task: {}, modelPath: {}, pullUrl: {}, pushUrl: {},classNames:{}", taskId, modelPath, pullUrl, pushUrl,  classNames);
        return detectionService.startTask(taskId, pullUrl, modelPath, pushUrl, classNames);
    }

    @GetMapping("/stop")
    public String stop() {
        // 默认停止task1
        String taskId = "task1";
        return detectionService.stopTask(taskId);
    }

    @GetMapping("/list")
    public Set<String> list() {
        return detectionService.listTasks();
    }

    @PostMapping("ping")
    public String ping() {
        return "pong123";
    }
}

